namespace NHCodeFirst.FluentMapper
{
    using System;
    using Enums;
    using Version = HbmModel.Version;

    public class VersionMapper : TypeAndColumnMapper<VersionMapper>
    {
        private readonly Version version;
        internal VersionMapper(Version version) : base(version.Type, version.Column)
        {
            this.version = version;
        }

        public VersionMapper Generated(VersionGenerated generated)
        {
            version.Generated = generated.ToString().ToLower();
            return this;
        }

        public VersionMapper Insert(bool value)
        {
            version.Insert = value.ToString().ToLower();
            return this;
        }

        public VersionMapper Access(Access access, Naming? namingStrategy = null)
        {
            return Access(access.ToString().ToLower(), namingStrategy);
        }

        public VersionMapper Access<T>(Naming? namingStrategy = null)
        {
            return Access(typeof(T).FullName, namingStrategy);
        }

        public VersionMapper Access(Type type, Naming? namingStrategy = null)
        {
            return Access(type.FullName, namingStrategy);
        }

        private VersionMapper Access(string access, Naming? naming)
        {
            version.Access = access;
            if (naming.HasValue)
                version.Access += "." + EnumsToHbmString.GetAccessNamingStrategy(naming.Value);
            return this;
        }

        public VersionMapper UnsavedValue(object value)
        {
            version.UnsavedValue = value == null ? "null" : value.ToString();
            return this;
        }
    }
}